/*
 * Copyright 2009 SHOP.COM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shop.opensocial.mediated.util;

import com.shop.opensocial.mediated.types.UserIdMediator;
import com.shop.opensocial.mediated.types.GroupIdMediator;
import com.shop.opensocial.mediated.types.SecurityTokenMediator;
import com.shop.opensocial.mediated.request.MediatedRequest;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;

/**
 * Utilities for getting User IDs
 *
 * @author Jordan Zimmerman
 */
public class UserIdUtils
{
	/**
	 * Converts from an external ID format to your internal format
	 *
	 * @param request request data
	 * @param converter ID converter
	 * @param user ID from OpenSocial/Shindig
	 * @param token security token
	 * @return internal ID
	 * @throws Exception errors
	 */
	public static String 		getIdFromUserId(MediatedRequest request, UserIdConverter converter, UserIdMediator user, SecurityTokenMediator token) throws Exception
	{
		return converter.convertId(request, user.getUserId(token));
	}

	/**
	 * Apply the given group and return the set of needed IDs
	 *
	 * @param request request data
	 * @param converter ID converter
	 * @param users users to get
	 * @param group group to get
	 * @param token security token
	 * @return set of internal IDs
	 * @throws Exception errors
	 */
	public static Set<String> 	getIdsFromUserIdsAndGroup(MediatedRequest request, UserIdConverter converter, Set<UserIdMediator> users, GroupIdMediator group, SecurityTokenMediator token) throws Exception
	{
		Set<String> ids = new HashSet<String>();
		for ( UserIdMediator user : users )
		{
			ids.addAll(getIdsFromUserIdAndGroup(request, converter, user, group, token));
		}
		return ids;
	}

	/**
	 * Apply the given group and return the set of needed IDs
	 *
	 * @param request request data
	 * @param converter ID converter
	 * @param user user to get
	 * @param group group to get
	 * @param token security token
	 * @return set of internal IDs
	 * @throws Exception errors
	 */
	public static Set<String> 	getIdsFromUserIdAndGroup(MediatedRequest request, UserIdConverter converter, UserIdMediator user, GroupIdMediator group, SecurityTokenMediator token) throws Exception
	{
		String 	userId = getIdFromUserId(request, converter, user, token);

		if ( group == null )
		{
			return new HashSet<String>(Arrays.asList(userId));
		}

		Set<String> ids = new HashSet<String>();
		switch ( group.getType() )
		{
			case ALL:
			case FRIENDS:
			{
				ids.addAll(converter.getFriends(request, userId));
				break;
			}

			case GROUPID:
			{
				ids.addAll(converter.getGroup(request, group.getGroupId()));
				break;
			}

			case SELF:
			{
				ids.add(userId);
				break;
			}

			default:
			{
				// ignore
				break;
			}
		}
		return ids;
	}

	private UserIdUtils()
	{
	}
}
